Neural Machine Translation

Welcome to your first programming assignment for this week!

This notebook was produced together with NVIDIA's Deep Learning Institute.

Updates

If you were working on the notebook before this update...

List of updates

Let's load all the packages you will need for this assignment.

1 - Translating human readable dates into machine readable dates

1.1 - Dataset

We will train the model on a dataset of 10,000 human readable dates and their equivalent, standardized, machine readable dates. Let's run the following cells to load the dataset and print some examples.

You've loaded:

Let's preprocess the data and map the raw text data into the index values.

You now have:

2 - Neural machine translation with attention

2.1 - Attention mechanism

In this part, you will implement the attention mechanism presented in the lecture videos.

</table>



**Figure 1**: Neural machine translation with attention
**Total params:** 52,960
**Trainable params:** 52,960
**Non-trainable params:** 0
**bidirectional_1's output shape ** (None, 30, 64)
**repeat_vector_1's output shape ** (None, 30, 64)
**concatenate_1's output shape ** (None, 30, 128)
**attention_weights's output shape ** (None, 30, 1)
**dot_1's output shape ** (None, 1, 64)
**dense_3's output shape ** (None, 11)

Compile the model

Sample code

optimizer = Adam(lr=..., beta_1=..., beta_2=..., decay=...)
model.compile(optimizer=..., loss=..., metrics=[...])

Define inputs and outputs, and fit the model

The last step is to define all your inputs and outputs to fit the model:

Let's now fit the model and run it for one epoch.

While training you can see the loss as well as the accuracy on each of the 10 positions of the output. The table below gives you an example of what the accuracies could be if the batch had 2 examples:


Thus, `dense_2_acc_8: 0.89` means that you are predicting the 7th character of the output correctly 89% of the time in the current batch of data.

We have run this model for longer, and saved the weights. Run the next cell to load our weights. (By training a model for several minutes, you should be able to obtain a model of similar accuracy, but loading our model will save you time.)

You can now see the results on new examples.

You can also change these examples to test with your own examples. The next part will give you a better sense of what the attention mechanism is doing--i.e., what part of the input the network is paying attention to when generating a particular output character.

3 - Visualizing Attention (Optional / Ungraded)

Since the problem has a fixed output length of 10, it is also possible to carry out this task using 10 different softmax units to generate the 10 characters of the output. But one advantage of the attention model is that each part of the output (such as the month) knows it needs to depend only on a small part of the input (the characters in the input giving the month). We can visualize what each part of the output is looking at which part of the input.

Consider the task of translating "Saturday 9 May 2018" to "2018-05-09". If we visualize the computed $\alpha^{\langle t, t' \rangle}$ we get this:


**Figure 8**: Full Attention Map

Notice how the output ignores the "Saturday" portion of the input. None of the output timesteps are paying much attention to that portion of the input. We also see that 9 has been translated as 09 and May has been correctly translated into 05, with the output paying attention to the parts of the input it needs to to make the translation. The year mostly requires it to pay attention to the input's "18" in order to generate "2018."

3.1 - Getting the attention weights from the network

Lets now visualize the attention values in your network. We'll propagate an example through the network, then visualize the values of $\alpha^{\langle t, t' \rangle}$.

To figure out where the attention values are located, let's start by printing a summary of the model .

Navigate through the output of model.summary() above. You can see that the layer named attention_weights outputs the alphas of shape (m, 30, 1) before dot_2 computes the context vector for every time step $t = 0, \ldots, T_y-1$. Let's get the attention weights from this layer.

The function attention_map() pulls out the attention values from your model and plots them.

On the generated plot you can observe the values of the attention weights for each character of the predicted output. Examine this plot and check that the places where the network is paying attention makes sense to you.

In the date translation application, you will observe that most of the time attention helps predict the year, and doesn't have much impact on predicting the day or month.

Congratulations!

You have come to the end of this assignment

Here's what you should remember

Congratulations on finishing this assignment! You are now able to implement an attention model and use it to learn complex mappings from one sequence to another.